package org.jeecg.modules.module.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xkcoding.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.module.constant.TypeConstant;
import org.jeecg.modules.module.constant.UrlConstant;
import org.jeecg.modules.module.mapper.StockMapper;
import org.jeecg.modules.module.model.Stock;
import org.jeecg.modules.module.service.IStockService;
import org.jeecg.modules.module.vo.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author chen1
 */
@Service
@Slf4j
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {

//    @Value("${byapi.licence}")
//    private String LICENCE;
    @Value("${waizao.token}")
    private String TOKEN;

    @Override
    public StockViewVo getView() {
        log.info("----------------开始获取大屏数据-----------------");

        StockViewVo stockViewVo = new StockViewVo();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String token = "&token=" + TOKEN;
        //获取国内指数
        Calendar gnzsCanlendar = Calendar.getInstance();
        gnzsCanlendar.add(Calendar.DAY_OF_YEAR,-1);
        Date gnzsYesterDay = gnzsCanlendar.getTime();
        Date gnzsNow = new Date();
        gnzsCanlendar.setTime(gnzsNow);
        int dayOfWeek = gnzsCanlendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.MONDAY){
            gnzsCanlendar.add(Calendar.DAY_OF_YEAR,-3);
            gnzsNow = gnzsCanlendar.getTime();
            gnzsYesterDay = gnzsCanlendar.getTime();
        }
        if (dayOfWeek == Calendar.SUNDAY){
            gnzsCanlendar.add(Calendar.DAY_OF_YEAR,-2);
            gnzsNow = gnzsCanlendar.getTime();
            gnzsYesterDay = gnzsCanlendar.getTime();
        }
        if (dayOfWeek == Calendar.SATURDAY){
            gnzsCanlendar.add(Calendar.DAY_OF_YEAR,-1);
            gnzsNow = gnzsCanlendar.getTime();
            gnzsYesterDay = gnzsCanlendar.getTime();
        }
        String gnzsTodayStr = format.format(gnzsNow);
        String gnzsYesterdayStr = format.format(gnzsYesterDay);
        String gnzsDateParam = "&startDate=" + gnzsYesterdayStr + "&endDate=" + gnzsTodayStr;
        log.info("----------获取国内指数请求 = {}",UrlConstant.GNZS_URL + token + gnzsDateParam);
        String gnzsStr = HttpUtil.get(UrlConstant.GNZS_URL + token + gnzsDateParam);
        JSONObject gnzsJson = JSON.parseObject(gnzsStr);
        String gnzsArrayStr = gnzsJson.getString("data");
        List gnzsList = JSONArray.parseObject(gnzsArrayStr,List.class);
        gnzsList = gnzsList.subList(0,49);
        log.debug("获取国内指数={}",JSON.toJSONString(gnzsList));
        stockViewVo.setGnzs(gnzsList);
        //获取模块指数
        Calendar mkzsCanlendar = Calendar.getInstance();
        mkzsCanlendar.add(Calendar.DAY_OF_YEAR,-1);
        Date mkzsYesterDay = mkzsCanlendar.getTime();
        Date mkzsNow = new Date();
        mkzsCanlendar.setTime(mkzsNow);
        int mkDayOfWeek = mkzsCanlendar.get(Calendar.DAY_OF_WEEK);
        if (mkDayOfWeek == Calendar.MONDAY){
            mkzsCanlendar.add(Calendar.DAY_OF_YEAR,-3);
            mkzsNow = mkzsCanlendar.getTime();
            mkzsYesterDay = mkzsCanlendar.getTime();
        }
        if (mkDayOfWeek == Calendar.SUNDAY){
            mkzsCanlendar.add(Calendar.DAY_OF_YEAR,-2);
            mkzsNow = mkzsCanlendar.getTime();
            mkzsYesterDay = mkzsCanlendar.getTime();
        }
        if (mkDayOfWeek == Calendar.SATURDAY){
            mkzsCanlendar.add(Calendar.DAY_OF_YEAR,-1);
            mkzsNow = mkzsCanlendar.getTime();
            mkzsYesterDay = mkzsCanlendar.getTime();
        }
        String mkzsTodayStr = format.format(mkzsNow);
        String mkzsYesterdayStr = format.format(mkzsYesterDay);
        String mkzsDateParam = "&startDate=" + mkzsYesterdayStr + "&endDate=" + mkzsTodayStr;
        log.info("----------获取模块指数请求 = {}",UrlConstant.MKZS_URL + token + mkzsDateParam);
        String mkzsStr = HttpUtil.get(UrlConstant.MKZS_URL + token + mkzsDateParam);
        JSONObject mkzsJson = JSON.parseObject(mkzsStr);
        String mkzsArrayStr = mkzsJson.getString("data");
        List mkzsList = JSONArray.parseObject(mkzsArrayStr,List.class);
        mkzsList = mkzsList.subList(0,49);
        stockViewVo.setMkzs(mkzsList);
        //获取外盘指数
        List wpzsList = new ArrayList<>();
        Map<String,String> wpzsMap1 = new HashMap<>();
        wpzsMap1.put("mc","道琼斯");
        wpzsMap1.put("zx","25742.65");
        wpzsMap1.put("zdf","1.05");
        wpzsMap1.put("zf","0.66");
        wpzsList.add(wpzsMap1);
        Map<String,String> wpzsMap2 = new HashMap<>();
        wpzsMap2.put("mc","标普500");
        wpzsMap2.put("zx","3080.82");
        wpzsMap2.put("zdf","1.05");
        wpzsMap2.put("zf","0.96");
        wpzsList.add(wpzsMap2);
        Map<String,String> wpzsMap3 = new HashMap<>();
        wpzsMap3.put("mc","纳斯达克");
        wpzsMap3.put("zx","9608.37");
        wpzsMap3.put("zdf","0.59");
        wpzsMap3.put("zf","1.46");
        wpzsList.add(wpzsMap3);
        Map<String,String> wpzsMap4 = new HashMap<>();
        wpzsMap4.put("mc","日经");
        wpzsMap4.put("zx","22574");
        wpzsMap4.put("zdf","1.11");
        wpzsMap4.put("zf","1.12");
        wpzsList.add(wpzsMap4);
        stockViewVo.setWpzs(wpzsList);
        //获取涨幅榜
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR,-1);
        Date startDate = calendar.getTime();
        Date endDate = new Date();
        int zfbDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (zfbDayOfWeek == Calendar.SUNDAY){
            calendar.add(Calendar.DAY_OF_YEAR,-2);
            startDate = calendar.getTime();
            endDate = calendar.getTime();
        }
        if (zfbDayOfWeek == Calendar.SATURDAY){
            calendar.add(Calendar.DAY_OF_YEAR,-1);
            startDate = calendar.getTime();
            endDate = calendar.getTime();
        }
        String startDateStr = format.format(startDate);
        String endDateStr = format.format(endDate);
        String zfbParam = "&startDate=" + startDateStr + "&endDate=" + endDateStr;
        log.info("----------获取涨幅榜请求 = {}",UrlConstant.ZFB_URL + token + zfbParam);
        String zfbStr = HttpUtil.get(UrlConstant.ZFB_URL + zfbParam + token);
        JSONObject zfbJson = JSON.parseObject(zfbStr);
        String zfbArrayStr = zfbJson.getString("data");
        List zfbList = JSONArray.parseObject(zfbArrayStr,List.class);
        zfbList = zfbList.subList(0,49);
        stockViewVo.setZfb(zfbList);
        //咨询新闻
        Calendar zxxwCanlendar = Calendar.getInstance();
        int zxxwDayOfWeek = zxxwCanlendar.get(Calendar.DAY_OF_WEEK);
        zxxwCanlendar.add(Calendar.DAY_OF_YEAR,-1);
        Date zxxwYesterDay = zxxwCanlendar.getTime();
        Date zxxwNow = new Date();
        zxxwCanlendar.setTime(mkzsNow);
        if (zxxwDayOfWeek == Calendar.MONDAY){
            zxxwCanlendar.add(Calendar.DAY_OF_YEAR,-3);
            zxxwNow = zxxwCanlendar.getTime();
            zxxwYesterDay = zxxwCanlendar.getTime();
        }
        if (zxxwDayOfWeek == Calendar.SUNDAY){
            zxxwCanlendar.add(Calendar.DAY_OF_YEAR,-2);
            zxxwNow = zxxwCanlendar.getTime();
            zxxwYesterDay = zxxwCanlendar.getTime();
        }
        if (zxxwDayOfWeek == Calendar.SATURDAY){
            zxxwCanlendar.add(Calendar.DAY_OF_YEAR,-1);
            zxxwNow = zxxwCanlendar.getTime();
            zxxwYesterDay = zxxwCanlendar.getTime();
        }
        String zxxwTodayStr = format.format(zxxwNow);
        String zxxwYesterdayStr = format.format(zxxwYesterDay);
        String zxxwDateParam = "&startDate=" + zxxwYesterdayStr + "&endDate=" + zxxwTodayStr;
        log.info("----------获取咨询新闻请求 = {}",UrlConstant.ZXXW_URL + token + zxxwDateParam);
        String zxxwStr = HttpUtil.get(UrlConstant.ZXXW_URL + token + zxxwDateParam);
        JSONObject zxxwJson = JSON.parseObject(zxxwStr);
        String zxxwArrayStr = zxxwJson.getString("data");
        List zxxwList = JSONArray.parseObject(zxxwArrayStr,List.class);
        zxxwList = zxxwList.subList(0,49);
        stockViewVo.setZxxw(zxxwList);
        //涨停跌停数
        calendar.add(Calendar.DAY_OF_YEAR,-5);
        Map<String,Integer> ztMap = new HashMap<>();
        Map<String,Integer> dtMap = new HashMap<>();
        for (int i = 0; i < 5; i++) {
            calendar.add(Calendar.DAY_OF_YEAR,1);
            Date date = calendar.getTime();
            String dateStr = format.format(date);
            String dateParam = "&startDate=" + dateStr + "&endDate=" + dateStr;
            log.info("----------获取涨停股池请求 = {}",UrlConstant.ZTGC_URL + token + dateParam);
            String ztgcStr = HttpUtil.get(UrlConstant.ZTGC_URL + dateParam + token);
            JSONObject ztgcJson = JSON.parseObject(ztgcStr);
            String ztgcArrayStr = ztgcJson.getString("data");
            List ztgcList = JSONArray.parseObject(ztgcArrayStr,List.class);
            log.info("----------获取跌停股池请求 = {}",UrlConstant.DTGC_URL + token + dateParam);
            String dtgcStr = HttpUtil.get(UrlConstant.DTGC_URL + dateParam + token);
            JSONObject dtgcJson = JSON.parseObject(dtgcStr);
            String dtgcArrayStr = dtgcJson.getString("data");
            List dtgcList = JSONArray.parseObject(dtgcArrayStr,List.class);
            ztMap.put(dateStr, CollUtil.isNotEmpty(ztgcList) ? ztgcList.size() : 0);
            dtMap.put(dateStr,CollUtil.isNotEmpty(dtgcList) ? dtgcList.size() : 0);
        }
        log.debug("获取涨停数={}",JSON.toJSONString(ztMap));
        log.debug("获取跌停数={}",JSON.toJSONString(dtMap));
        stockViewVo.setZts(ztMap);
        stockViewVo.setDts(dtMap);
        //昨日和今日成交量
        String tomorrowDayStr = format.format(new Date());
        calendar.setTime(new Date());
        Date todayDate;
        Date yestardayDate;
        int cjlDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (cjlDayOfWeek == Calendar.MONDAY){
            calendar.add(Calendar.DAY_OF_YEAR,-3);
            todayDate = calendar.getTime();
            calendar.add(Calendar.DAY_OF_YEAR,-1);
            yestardayDate = calendar.getTime();
        }else if (cjlDayOfWeek == Calendar.SUNDAY){
            calendar.add(Calendar.DAY_OF_YEAR,-2);
            todayDate = calendar.getTime();
            calendar.add(Calendar.DAY_OF_YEAR,-1);
            yestardayDate = calendar.getTime();
        }else if (cjlDayOfWeek == Calendar.SATURDAY){
            calendar.add(Calendar.DAY_OF_YEAR,-1);
            todayDate = calendar.getTime();
            calendar.add(Calendar.DAY_OF_YEAR,-1);
            yestardayDate = calendar.getTime();
        }else {
            calendar.add(Calendar.DAY_OF_YEAR,-1);
            todayDate = calendar.getTime();
            calendar.add(Calendar.DAY_OF_YEAR,-1);
            yestardayDate = calendar.getTime();
        }
        String todayStr = format.format(todayDate);
        String yesterdayStr = format.format(yestardayDate);
        String todayParamStr = "startDate=" + todayStr + "&endDate=" + tomorrowDayStr;
        String yesterdayParamStr = "startDate=" + yesterdayStr + "&endDate=" + todayStr;
        log.info("----------获取今日成交量请求 = {}",UrlConstant.JRCJL_URL + token + todayParamStr);
        String todayCjlStr = HttpUtil.get(UrlConstant.JRCJL_URL + todayParamStr + token);
        log.info("----------获取昨日成交量请求 = {}",UrlConstant.JRCJL_URL + token + yesterdayParamStr);
        String yesterdayCjlStr = HttpUtil.get(UrlConstant.JRCJL_URL + yesterdayParamStr + token);
        JSONObject todayJson = JSON.parseObject(todayCjlStr);
        JSONObject yesterdayJson = JSON.parseObject(yesterdayCjlStr);
        JSONArray todayJsonJSONArray = todayJson.getJSONArray("data");
        JSONArray yesterdayJsonJSONArray = yesterdayJson.getJSONArray("data");
        Map<String,String> todayCjl = new HashMap<>();
        Map<String,String> yesterdayCjl = new HashMap<>();
        if (ObjectUtil.isNotNull(todayJsonJSONArray)){
            todayJsonJSONArray.forEach(t -> todayCjl.put(((JSONObject)t).getString("tdate"),((JSONObject)t).getString("cjl")));
        }
        if (ObjectUtil.isNotNull(yesterdayJsonJSONArray)){
            yesterdayJsonJSONArray.forEach(t -> yesterdayCjl.put(((JSONObject)t).getString("tdate"),((JSONObject)t).getString("cjl")));
        }
        log.debug("昨日成交量={}",JSON.toJSONString(yesterdayCjl));
        log.debug("今日成交量={}",JSON.toJSONString(todayCjl));
        stockViewVo.setTodayCjl(todayCjl);
        stockViewVo.setYesterdayCjl(yesterdayCjl);
        //大盘股票涨跌停
        String zdtslParamStr = "startDate=" + todayStr + "&endDate=" + todayStr;
        log.info("----------获取股票涨跌停数量请求 = {}",UrlConstant.ZDTSL_URL + token + zdtslParamStr);
        String zdtslStr = HttpUtil.get(UrlConstant.ZDTSL_URL + zdtslParamStr + token);
        JSONObject zdtslJson = JSON.parseObject(zdtslStr);
        JSONArray zdtslJSONArray = zdtslJson.getJSONArray("data");
        AtomicInteger minusThreeToZero = new AtomicInteger(0);
        AtomicInteger minusFiveToThree = new AtomicInteger(0);
        AtomicInteger minusSevenToFive = new AtomicInteger(0);
        AtomicInteger zeroToThree = new AtomicInteger(0);
        AtomicInteger threeToFive = new AtomicInteger(0);
        AtomicInteger fiveToSeven = new AtomicInteger(0);
        AtomicInteger lessMinusSeven = new AtomicInteger(0);
        AtomicInteger greaterSeven = new AtomicInteger(0);
        BigDecimal zero = BigDecimal.ZERO;
        BigDecimal minusThree = new BigDecimal("-0.3");
        BigDecimal minusFive = new BigDecimal("-0.5");
        BigDecimal minusSeven = new BigDecimal("-0.7");
        BigDecimal three = new BigDecimal("0.3");
        BigDecimal five = new BigDecimal("0.5");
        BigDecimal seven = new BigDecimal("0.7");
        if (ObjectUtil.isNotNull(zdtslJSONArray)){
            zdtslJSONArray.forEach(z -> {
                BigDecimal zdfd = ((JSONObject) z).getBigDecimal("zdfd");
                if (zdfd.compareTo(minusSeven) < 0){
                    //小于-7
                    lessMinusSeven.incrementAndGet();
                }else if (zdfd.compareTo(minusSeven) >= 0 && zdfd.compareTo(minusFive) < 0){
                    // 大于-7 小于-5
                    minusSevenToFive.incrementAndGet();
                }else if (zdfd.compareTo(minusFive) >= 0 && zdfd.compareTo(minusThree) < 0){
                    // 大于-5 小于-3
                    minusFiveToThree.incrementAndGet();
                }else if (zdfd.compareTo(minusThree) >= 0 && zdfd.compareTo(zero) < 0){
                    // 大于-3 小于0
                    minusThreeToZero.incrementAndGet();
                }else if (zdfd.compareTo(zero) >= 0 && zdfd.compareTo(three) < 0){
                    // 大于0 小于3
                    zeroToThree.incrementAndGet();
                }else if (zdfd.compareTo(three) >= 0 && zdfd.compareTo(five) < 0){
                    // 大于3 小于5
                    threeToFive.incrementAndGet();
                }else if (zdfd.compareTo(five) >= 0 && zdfd.compareTo(seven) < 0){
                    // 大于5 小于7
                    fiveToSeven.incrementAndGet();
                }else if (zdfd.compareTo(seven) < 0){
                    // 大于7
                    greaterSeven.incrementAndGet();
                }
            });
        }
        Map<String, Integer> zdtslMap = new HashMap<>();
        zdtslMap.put("lessMinusSeven",lessMinusSeven.intValue());
        zdtslMap.put("minusSevenToFive",minusSevenToFive.intValue());
        zdtslMap.put("minusFiveToThree",minusFiveToThree.intValue());
        zdtslMap.put("minusThreeToZero",minusThreeToZero.intValue());
        zdtslMap.put("zeroToThree",zeroToThree.intValue());
        zdtslMap.put("threeToFive",threeToFive.intValue());
        zdtslMap.put("fiveToSeven",fiveToSeven.intValue());
        zdtslMap.put("greaterSeven",greaterSeven.intValue());
//        log.debug("大盘股票涨跌停={}",JSON.toJSONString(zdtslMap));
        stockViewVo.setZdtsl(zdtslMap);
        log.info("----------------获取大屏数据结束-----------------");
        return stockViewVo;
    }

    @Override
    public List<StockListVo> getList() {
        log.info("----------------开始获取股票列表数据-----------------");
        String code = "&code=all";
        log.info("----------------获取股票列表请求 = {}" , UrlConstant.GPLB_URL + TOKEN + code);
        String gplbStr = HttpUtil.get(UrlConstant.GPLB_URL + TOKEN + code);
        JSONObject gplbJson = JSON.parseObject(gplbStr);
        JSONArray gplbArray = gplbJson.getJSONArray("data");
        List<StockListVo> list = new ArrayList<>();
        if (CollUtil.isNotEmpty(gplbArray)){
            gplbArray = new JSONArray(gplbArray.subList(0, 500));
            gplbArray.forEach(g -> {
                JSONObject gp = (JSONObject) g;
                StockListVo vo = new StockListVo();
                vo.setCode(gp.getString("code"));
                vo.setName(gp.getString("name"));
                vo.setZrspj(gp.getBigDecimal("zrspj"));
                vo.setOpen(gp.getBigDecimal("open"));
                vo.setPrice(gp.getBigDecimal("price"));
                vo.setHigh(gp.getBigDecimal("high"));
                vo.setLow(gp.getBigDecimal("low"));
                vo.setCje(gp.getBigDecimal("cje"));
                vo.setCjl(gp.getBigDecimal("cjl").intValue());
                list.add(vo);
            });
        }
        log.info("----------------获取股票列表数据结束-----------------");
        return list;
    }

    @Override
    public StockDetailVo detail(String code, String type) {
        log.info("----------------开始获取股票详情数据-----------------");
        String token = "&token=" + TOKEN;
        StockDetailVo vo = new StockDetailVo();
        //获取公司信息
        code = "&code=" + code;
        log.info("----------------获取公司信息请求 = {}" , UrlConstant.GSXX_URL + token + code);
        String gsxxStr = HttpUtil.get(UrlConstant.GSXX_URL + code + token);
        JSONObject gsxxJson = JSON.parseObject(gsxxStr);
        JSONArray gsxxArray = gsxxJson.getJSONArray("data");
        if (gsxxArray.isEmpty()){
            throw new RuntimeException("该股票不存在！");
        }
        JSONObject gsxxObject = gsxxArray.getJSONObject(0);
        vo.setName(gsxxObject.getString("name"));
        vo.setMainBusiness(gsxxObject.getString("mainbusin"));
        vo.setBk(gsxxObject.getString("bk"));
        //获取公司实时行情信息
        log.info("----------------获取股票列表请求 = {}" , UrlConstant.GPLB_URL + TOKEN + code);
        String gsssxxStr = HttpUtil.get(UrlConstant.GPLB_URL  + TOKEN + code );
        JSONObject gsssxxJson = JSON.parseObject(gsssxxStr);
        JSONArray gsssxxArray = gsssxxJson.getJSONArray("data");
        JSONObject gsssxxObject = gsssxxArray.getJSONObject(0);
        CompanyRealTimeVo companyVo = new CompanyRealTimeVo();
        companyVo.setCode(gsssxxObject.getString("code"));
        companyVo.setName(gsssxxObject.getString("name"));
        companyVo.setZrspj(gsssxxObject.getBigDecimal("zrspj"));
        companyVo.setOpen(gsssxxObject.getBigDecimal("open"));
        companyVo.setPrice(gsssxxObject.getBigDecimal("price"));
        companyVo.setHigh(gsssxxObject.getBigDecimal("high"));
        companyVo.setLow(gsssxxObject.getBigDecimal("low"));
        companyVo.setCje(gsssxxObject.getBigDecimal("cje"));
        companyVo.setCjl(gsssxxObject.getBigDecimal("cjl").intValue());
        vo.setGssssj(companyVo);
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat minuteFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取最新时间和成交量
        List<StockMinuteVo> minuteList = new ArrayList<>();
        calendar.add(Calendar.DAY_OF_YEAR,-1);
        Date endDate = calendar.getTime();
        String endDateStr = minuteFormat.format(endDate);
        calendar.add(Calendar.MINUTE,-4);
        calendar.set(Calendar.SECOND,0);
        Date minuteStartDate = calendar.getTime();
        String minuteStartDateStr = minuteFormat.format(minuteStartDate);
        String minuteDateParam = "&startDate=" + minuteStartDateStr + "&endDate=" + endDateStr;
        minuteDateParam = minuteDateParam.replaceAll(" ", "%20");
        log.info("----------------获取股票分线信息请求 = {}" , UrlConstant.FXXX_URL + code + minuteDateParam + token);
        String fxxxStr = HttpUtil.get(UrlConstant.FXXX_URL + code + minuteDateParam + token);
        JSONObject fxxxJson = JSON.parseObject(fxxxStr);
        JSONArray fxxxArray = fxxxJson.getJSONArray("data");
        fxxxArray.forEach( f ->{
            JSONObject minute = (JSONObject) f;
            StockMinuteVo minuteVo = new StockMinuteVo();
            minuteVo.setTdate(minute.getString("tdate"));
            minuteVo.setCjl(minute.getBigDecimal("cjl"));
            minuteVo.setCjjj(minute.getBigDecimal("cjjj"));
            minuteVo.setCje(minute.getBigDecimal("cje"));
            minuteList.add(minuteVo);
        });
        vo.setMinuteList(minuteList);
        //获取K线数据
        Date endHourDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<KVo> kList = new ArrayList<>();
        //K线类型为时线
        if (TypeConstant.HOUR.equals(type)){
            String endHourStr = format.format(endHourDate);
            calendar.add(Calendar.DAY_OF_YEAR,-59);
            Date startHourDate = calendar.getTime();
            String startHourStr = format.format(startHourDate);
            String hourParam = "&startDate=" + startHourStr + "&endDate=" + endHourStr;
            log.info("----------------获取股票时线信息请求 = {}" , UrlConstant.HOUR_URL + code + hourParam + token);
            String hourStr = HttpUtil.get(UrlConstant.HOUR_URL + code + hourParam + token);
            JSONObject hourJson = JSON.parseObject(hourStr);
            JSONArray data = hourJson.getJSONArray("data");
            data.forEach(d -> {
                JSONObject hour = (JSONObject) d;
                KVo k = new KVo();
                k.setCode(hour.getString("code"));
                k.setName(hour.getString("name"));
                k.setKtype(hour.getInteger("ktype"));
                k.setTdate(hour.getString("tdate"));
                k.setOpen(hour.getBigDecimal("open"));
                k.setClose(hour.getBigDecimal("close"));
                k.setHigh(hour.getBigDecimal("high"));
                k.setLow(hour.getBigDecimal("low"));
                k.setHsl(hour.getBigDecimal("hsl"));
                k.setCje(hour.getBigDecimal("cje"));
                k.setCjl(hour.getBigDecimal("cjl").intValue());
                kList.add(k);
            });
            vo.setKList(kList);
            return vo;
        }
        String kType = "&ktype=" + type;
        calendar.setTime(new Date());
        String endStr = format.format(new Date());
        //K线类型为日线
        if (TypeConstant.DAY.equals(type)){
            calendar.add(Calendar.DAY_OF_YEAR,-365);
        }
        //K线类型为周线
        if (TypeConstant.WEEK.equals(type)){
            calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
            calendar.add(Calendar.DAY_OF_YEAR,-7 * 180);
        }
        //K线类型为月线
        if (TypeConstant.MONTH.equals(type)){
            calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            calendar.add(Calendar.MONTH,-100);
        }
        Date startDate = calendar.getTime();
        String startDateStr = format.format(startDate);
        String dateParam = "&startDate=" + startDateStr + "&endDate=" + endStr;
        log.info("----------------获取股票日线信息请求 = {}" , UrlConstant.DAY_URL + code + kType + dateParam + token);
        String dayStr = HttpUtil.get(UrlConstant.DAY_URL + code + kType + dateParam + token);
        JSONObject dayJson = JSON.parseObject(dayStr);
        JSONArray data = dayJson.getJSONArray("data");
        data.forEach(d -> {
            JSONObject hour = (JSONObject) d;
            KVo k = new KVo();
            k.setCode(hour.getString("code"));
            k.setName(hour.getString("name"));
            k.setKtype(hour.getInteger("ktype"));
            k.setTdate(hour.getString("tdate"));
            k.setOpen(hour.getBigDecimal("open"));
            k.setClose(hour.getBigDecimal("close"));
            k.setHigh(hour.getBigDecimal("high"));
            k.setLow(hour.getBigDecimal("low"));
            k.setHsl(hour.getBigDecimal("hsl"));
            k.setCje(hour.getBigDecimal("cje"));
            k.setCjl(hour.getBigDecimal("cjl").intValue());
            kList.add(k);
        });
        vo.setKList(kList);
        log.info("----------------获取股票详情数据结束-----------------");
        return vo;
    }
}
